import 'dart:convert';

import 'package:buy_flutter/net/bean/UnitGroupBean.dart';
import 'package:buy_flutter/net/net_manager.dart';
import 'package:buy_flutter/net/net_url.dart';
import 'package:buy_flutter/page/main/bloc/main_bloc.dart';
import 'package:buy_flutter/router/router_utils.dart';
import 'package:buy_flutter/router/routes.dart';
import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

class MainGroupView extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _MainGroupState();
  }
}

class _MainGroupState extends State {
  List<Widget> childrens = [];

  @override
  void initState() {
    super.initState();
    getGroups();
  }

  @override
  void didUpdateWidget(covariant StatefulWidget oldWidget) {
    super.didUpdateWidget(oldWidget);
    getGroups();
  }

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<MainBloc, MainState>(
      buildWhen: (previous, current) {
        if (current.unitLists != null && current.unitLists.length > 0) {
          return true;
        }
        return false;
      },
      builder: (context, state) {
        return Column(
          children: childrens,
        );
      },
    );
  }

  void getGroups() async {
    try {
      Response<String> response =
          await NetManager.getInstance().get<String>(NetUrls.get_groups);
      String resStr = response.data;
      print(resStr);

      UnitGroupBean unitGroupBean = UnitGroupBean.fromJson(json.decode(resStr));
      print("unitGroupBean=======>" + unitGroupBean.message);
      if (unitGroupBean.code == 200 && unitGroupBean.data.length > 0) {
        childrens = [];
        for (int i = 0; i < unitGroupBean.data.length; i++) {
          UnitBean result = unitGroupBean.data[i];
          if (i == 0) {
            childrens.add(_itemUnitView(0xFF1B83C6, "杨 帆", "階段素養：建構及動機",
                result.groupBuy, result.id.toString(),i));
          } else if (i == 1) {
            childrens.add(_itemUnitView(0xFFE89100, "綻 放", "階段素養：參與及探索",
                result.groupBuy, result.id.toString(),i));
          } else if (i == 2) {
            childrens.add(_itemUnitView(0xFF009300, "拔 萃", "階段素養：應用及提升",
                result.groupBuy, result.id.toString(),i));
          }
        }
        context.read<MainBloc>().add(MainUnitViewChanged(childrens));
      }
    } catch (e) {
      print("e=======>" + e);
    }
  }

  Widget _itemUnitView(
      int borderColor, String name, String des, bool isBuy, String groupId,int index) {
    return Container(
      margin: EdgeInsets.only(top: 20, left: 25, right: 25),
      padding: EdgeInsets.only(left: 20, right: 20, top: 15, bottom: 15),
      width: double.infinity,
      decoration: BoxDecoration(
          color: Colors.white,
          border: Border.all(color: Color(borderColor), width: 3),
          borderRadius: BorderRadius.circular(8),
          boxShadow: [
            BoxShadow(
              blurRadius: 20,
              offset: Offset.fromDirection(0.5),
              spreadRadius: 2,
              color: Colors.grey,
            )
          ]),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          RichText(
              text: TextSpan(children: [
            TextSpan(
                text: name,
                style: TextStyle(
                    fontSize: 34,
                    color: Color(borderColor),
                    fontWeight: FontWeight.bold)),
            TextSpan(
                text: " 阶段",
                style: TextStyle(
                    fontSize: 30,
                    color: Color(borderColor),
                    fontWeight: FontWeight.bold)),
          ])),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Expanded(
                child: Text(
                  des,
                  maxLines: 2,
                  style: TextStyle(
                    fontSize: 16,
                    color: Color(0xFF6F6F6F),
                  ),
                ),
              ),
              isBuy
                  ? GestureDetector(
                      child: Container(
                        height: 32,
                        width: 115,
                        margin: EdgeInsets.only(top: 5),
                        alignment: Alignment.center,
                        decoration: BoxDecoration(
                          color: Color(0xFFA00AA9),
                          borderRadius: BorderRadius.circular(15),
                        ),
                        child: Text(
                          "开始学习",
                          style: TextStyle(
                            color: Colors.white,
                            fontSize: 16,
                          ),
                        ),
                      ),
                      onTap: () {
                        jumpGroupDetail(groupId,index);
                      },
                    )
                  : Container(
                      height: 32,
                      width: 115,
                      margin: EdgeInsets.only(top: 5),
                      alignment: Alignment.center,
                      decoration: BoxDecoration(
                          color: Colors.white,
                          borderRadius: BorderRadius.circular(15),
                          border:
                              Border.all(color: Color(0xff979797), width: 1)),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Image.asset(
                            "assets/main/ic_main_type_lock.png",
                            width: 17,
                            height: 17,
                          ),
                          Padding(
                            padding: const EdgeInsets.symmetric(horizontal: 8),
                            child: Text(
                              "锁定",
                              style: TextStyle(
                                color: Color(0xff979797),
                                fontSize: 16,
                              ),
                            ),
                          )
                        ],
                      ),
                    )
            ],
          ),
        ],
      ),
    );
  }

  void jumpGroupDetail(String groupId, int index) {
    RouterUtils.goToNativePage(
        Routes.groupDetail, {"groupId": groupId, "index": index.toString()});
  }
}
